package com.fei.spring.rabbitmq.four;


import com.fei.spring.rabbitmq.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;

import java.util.UUID;

//验证发布确认模式、单个确认
public class Individually {
    //批量发1000个消息
    public static int MESSAGE_COUNT = 1000;
    public static void main(String[] args) throws Exception {
        //1、单个确认
        Channel channel = RabbitMQUtils.getChannel();
        //队列声明
        String queueName = UUID.randomUUID().toString();
        channel.queueDeclare(queueName,true,false,false,null);
        //开启发布确认
        channel.confirmSelect();
        //开始时间
        long begin = System.currentTimeMillis();
        //批量发消息
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message = i + "";
            channel.basicPublish("",queueName,null,message.getBytes());
            //单个消息马上进行发布确认
            boolean flag = channel.waitForConfirms();
            if (flag){
                System.out.println("消息发送成功");
            }
        }
        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("发布"+MESSAGE_COUNT+"个单独确认消息，耗时" + (end - begin) + "ms");
    }
}